﻿using App_Clinique.ClassLibrary.Classes;
using App_Clinique.ClassLibrary.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App_Clinique
{
    public partial class PlanificationForm : Form
    {
        public Consultation ConsultationDB { get; set; }
        public Dictionary<String, TimeSpan> Horaires = new Dictionary<String, TimeSpan>();

        public PlanificationForm()
        {
            InitializeComponent();

            Horaires.Add("08:00", new TimeSpan(8, 0, 0));
            Horaires.Add("08:30", new TimeSpan(8, 30, 0));
            Horaires.Add("09:00", new TimeSpan(9, 0, 0));
            Horaires.Add("09:30", new TimeSpan(9, 30, 0));
            Horaires.Add("10:00", new TimeSpan(10, 0, 0));
            Horaires.Add("10:30", new TimeSpan(10, 30, 0));
            Horaires.Add("11:00", new TimeSpan(11, 0, 0));
            Horaires.Add("11:30", new TimeSpan(11, 30, 0));
            Horaires.Add("13:00", new TimeSpan(13, 0, 0));
            Horaires.Add("13:30", new TimeSpan(13, 30, 0));
            Horaires.Add("14:00", new TimeSpan(14, 0, 0));
            Horaires.Add("14:30", new TimeSpan(14, 30, 0));
            Horaires.Add("15:00", new TimeSpan(15, 0, 0));
            Horaires.Add("15:30", new TimeSpan(15, 30, 0));
            Horaires.Add("16:00", new TimeSpan(16, 0, 0));
            Horaires.Add("16:30", new TimeSpan(16, 30, 0));
            Horaires.Add("17:00", new TimeSpan(17, 0, 0));
            Horaires.Add("17:30", new TimeSpan(17, 30, 0));
            Horaires.Add("18:00", new TimeSpan(18, 0, 0));
            Horaires.Add("18:30", new TimeSpan(18, 30, 0));
        }

        /****************************************************************************************************
         * Methods
         ***************************************************************************************************/
        private void InitializePatientsComboBox()
        {
            try
            {
                Dictionary<String, Patient> PatientItems = new Dictionary<String, Patient>();

                Singleton.Current.Patients.ForEach(PatientDB => {
                    String PatientName = String.Format("{0} {1} {2}", PatientDB.Civilite, PatientDB.Prenom, PatientDB.Nom);
                    PatientItems.Add(PatientName, PatientDB);
                });

                PatientComboBox.DataSource = new BindingSource(PatientItems, null);
                PatientComboBox.DisplayMember = "key";
                PatientComboBox.ValueMember = "value";
            }
            catch (InvalidCastException e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void InitializeHorairesComboBox()
        {
            try
            {
                Dictionary<String, TimeSpan> HorairesItems = new Dictionary<string,TimeSpan>(Horaires);

                List<Consultation> ConsultationsDB = Consultation.GetListOfThisDay(DateConsultationDateTimePicker.Value.Date);
                
                switch (Singleton.Current.ModeCourant)
                {
                    case Singleton.Mode.Consultation:
                        HorairesItems.ToList().ForEach(Horaire =>
                        {
                            bool PrisPourCetteConsultation = (ConsultationDB.DateConsultationPrevue.TimeOfDay.Ticks == Horaire.Value.Ticks);

                            if (!PrisPourCetteConsultation)
                            {
                                HorairesItems.Remove(Horaire.Key);
                            }
                        });
                        break;

                    case Singleton.Mode.Modifier:
                        HorairesItems.ToList().ForEach(Horaire =>
                        {
                            DateTime _Horaire = DateConsultationDateTimePicker.Value.Date.AddTicks(Horaire.Value.Ticks);
                            bool DejaReserve = (ConsultationsDB.Where(c => c.DateConsultationPrevue == _Horaire).Count() > 0);
                            bool PrisPourCetteConsultation = (ConsultationDB.DateConsultationPrevue.Ticks == Horaire.Value.Ticks);

                            if (DejaReserve && !PrisPourCetteConsultation)
                            {
                                HorairesItems.Remove(Horaire.Key);
                            }
                        });
                        break;

                    case Singleton.Mode.Nouveau:
                        HorairesItems.ToList().ForEach(Horaire =>
                        {
                            DateTime _Horaire = DateConsultationDateTimePicker.Value.Date.AddTicks(Horaire.Value.Ticks);
                            bool DejaReserve = (ConsultationsDB.Where(c => c.DateConsultationPrevue == _Horaire).Count() > 0);
                            
                            if (DejaReserve)
                            {
                                HorairesItems.Remove(Horaire.Key);
                            }
                        });
                        break;
                }

                HeureConsultationComboBox.DataSource = new BindingSource(HorairesItems, null);
                HeureConsultationComboBox.DisplayMember = "key";
                HeureConsultationComboBox.ValueMember = "value";
            }
            catch (InvalidCastException e)
            {
                MessageBox.Show(e.Message);
            }
        }

        /****************************************************************************************************
         * Events
         ***************************************************************************************************/
        private void PlanificationForm_Load(object sender, EventArgs e)
        {
            InitializePatientsComboBox();
            InitializeHorairesComboBox();

            switch (Singleton.Current.ModeCourant)
            {
                case Singleton.Mode.Nouveau:
                    PatientComboBox.Enabled = true;
                    DateConsultationDateTimePicker.Enabled = true;
                    HeureConsultationComboBox.Enabled = true;
                    SupprimerButton.Enabled = false;
                    SauvegarderButton.Enabled = true;
                    break;

                case Singleton.Mode.Modifier:
                case Singleton.Mode.Consultation:
                    PatientComboBox.SelectedItem = ConsultationDB.Patient;
                    DateConsultationDateTimePicker.Value = ConsultationDB.DateConsultationPrevue.Date;
                    HeureConsultationComboBox.SelectedItem = Horaires.AsQueryable().Where(h => h.Value.Ticks == ConsultationDB.DateConsultationPrevue.TimeOfDay.Ticks).SingleOrDefault();
                    break;
            }
        }

        private void DateConsultationDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            InitializeHorairesComboBox();
        }

        private void AjouterButton_Click(object sender, EventArgs e)
        {
            Singleton.Current.ModeCourant = Singleton.Mode.Nouveau;

            ConsultationDB = new Consultation();

            PatientComboBox.Enabled = true;
            DateConsultationDateTimePicker.Enabled = true;
            HeureConsultationComboBox.Enabled = true;
            SupprimerButton.Enabled = false;
            SauvegarderButton.Enabled = true;

            InitializeHorairesComboBox();

            PatientComboBox.SelectedIndex = 0;
            DateConsultationDateTimePicker.Value = DateTime.Now;
            HeureConsultationComboBox.SelectedIndex = 0;
        }

        private void ModifierButton_Click(object sender, EventArgs e)
        {
            Singleton.Current.ModeCourant = Singleton.Mode.Modifier;

            PatientComboBox.Enabled = true;
            DateConsultationDateTimePicker.Enabled = true;
            HeureConsultationComboBox.Enabled = true;
            SauvegarderButton.Enabled = true;

            InitializeHorairesComboBox();
        }

        private void SupprimerButton_Click(object sender, EventArgs e)
        {
            Singleton.Current.ModeCourant = Singleton.Mode.Supprimer;

            Consultation.Delete(ConsultationDB);
            ConsultationDB = new Consultation();

            PatientComboBox.SelectedIndex = 0;
            DateConsultationDateTimePicker.Value = DateTime.Now.Date;
            InitializeHorairesComboBox();

            Singleton.Current.Consultations = Consultation.GetList();
            Singleton.Current.ConsultationsDeLaSemaine = Consultation.GetListOfWeek();

            MainForm EcranPrincipal = new MainForm();
            EcranPrincipal.RefreshPlanificationDataGridView();
        }

        private void SauvegarderButton_Click(object sender, EventArgs e)
        {
            DateTime JourChoisi = DateConsultationDateTimePicker.Value.Date;
            TimeSpan HeureChoisie = ((KeyValuePair<String, TimeSpan>)HeureConsultationComboBox.SelectedItem).Value;
            DateTime HoraireChoisi = JourChoisi.AddTicks(HeureChoisie.Ticks);
            ConsultationDB.DateConsultationPrevue = HoraireChoisi;
            ConsultationDB.DateConsultationReelle = null;
            ConsultationDB.PatientID = ((Patient)((KeyValuePair<String, Patient>)PatientComboBox.SelectedItem).Value).PatientID;

            switch (Singleton.Current.ModeCourant)
            {
                case Singleton.Mode.Nouveau:
                    Consultation.Create(ConsultationDB);
                    MessageBox.Show("Consultation créee");
                    break;

                case Singleton.Mode.Modifier:
                    Consultation.Update(ConsultationDB);
                    MessageBox.Show("Consultation modifiée");
                    break;
            }

            Singleton.Current.ModeCourant = Singleton.Mode.Consultation;

            PatientComboBox.Enabled = false;
            DateConsultationDateTimePicker.Enabled = false;
            HeureConsultationComboBox.Enabled = false;
            SupprimerButton.Enabled = true;
            SauvegarderButton.Enabled = false;

            InitializeHorairesComboBox();

            Singleton.Current.Consultations = Consultation.GetList();
            Singleton.Current.ConsultationsDeLaSemaine = Consultation.GetListOfWeek();

            MainForm EcranPrincipal = new MainForm();
            EcranPrincipal.RefreshPlanificationDataGridView();
        }
    }
}
